package common;

import java.io.IOException;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.SimpleTagSupport;

/**
 * 分页标签类
 * 
 * @author 付晓辉
 * 
 */
public class PageTag extends SimpleTagSupport {
	// 后台分页显示的action或者servlet的路径
	private String path;
	// 分页类的信息
	private PageBean pageBean;
	//分页条的formId
	private String pageFormId;
	// 分页的参数
	private Map<String, String> paramMap;
	public void doTag() throws JspException, IOException {
		JspWriter out=this.getJspContext().getOut();
		StringBuffer pageHtml=new StringBuffer();
		String link;
		pageHtml.append("<form id=\""+pageFormId+"\" action=\""+path+"\" method=\"post\">");
		pageHtml.append("<input type=\"hidden\" id=\"currentPage\" name=\"pageBean.currentPage\" value=\""+pageBean.getCurrentPage()+"\">");
		pageHtml.append("<input type=\"hidden\" id=\"pageCount\" name=\"pageBean.pageCount\" value=\""+pageBean.getPageCount()+"\">");
		pageHtml.append("<input type=\"hidden\" id=\"rowsCount\" name=\"pageBean.rowsCount\" value=\""+pageBean.getRowsCount()+"\">");
		pageHtml.append("<input type=\"hidden\" id=\"pageSize\" name=\"pageBean.pageSize\" value=\""+pageBean.getPageSize()+"\">");
		pageHtml = getParams(pageHtml);
		pageHtml.append("<div class=\"pagediv\">");
		pageHtml.append("<div class=\"message\">");
		pageHtml.append("共<i class=\"blue\">"+pageBean.getRowsCount()+"</i>条记录,当前显示第<i class=\"blue\" >"+pageBean.getCurrentPage()+"</i>页");
		pageHtml.append("</div>");
		pageHtml.append("<div class=\"pagin\" style=\"float:right;\">");
		pageHtml.append("<ul class=\"paginList\">");
		if(path.indexOf("|")>0){
			path=path.replace("|", "','");
			link="<a href=\"javascript:void(0);\" onclick=\"ajaxTurning(%s,'"+path+"','"+pageFormId+"');\">%s</a>";
		}else{
			link="<a href=\"javascript:void(0);\" onclick=\"pageTurning(%s,'"+pageFormId+"');\">%s</a>";
		}
		if(pageBean.getCurrentPage()==1){
			pageHtml.append("<li class=\"paginItem disabled\">");
			
		}else{
			pageHtml.append("<li class=\"paginItem\">");
		}
		pageHtml.append(String.format(link, pageBean.getCurrentPage()<=1?1:pageBean.getCurrentPage()-1+"","<span class=\"pagepre\" style=\"display: block;\"></span>"));
		pageHtml.append("</li>");
		/*****************数字上下各输出三个*************************/
		int star=pageBean.getCurrentPage()-3<1?1:pageBean.getCurrentPage()-3;
		int end=pageBean.getCurrentPage()+3>pageBean.getPageCount()?pageBean.getPageCount():pageBean.getCurrentPage()+3;
		for(int j=star;j<=end;j++){
			if(j==pageBean.getCurrentPage()){
				pageHtml.append("<li class=\"paginItem current\">");
			}else{
				pageHtml.append("<li class=\"paginItem\">");
			}
			pageHtml.append(String.format(link, j+"",j+""));
			pageHtml.append("</li>");
		}
		/*********************末页********************/
		if(pageBean.getCurrentPage()==pageBean.getPageCount()){
			pageHtml.append("<li class=\"paginItem disabled\">");
		}else{
			pageHtml.append("<li class=\"paginItem\">");
		}
		pageHtml.append(String.format(link, pageBean.getCurrentPage()>=pageBean.getPageCount()?pageBean.getPageCount():pageBean.getCurrentPage()+1+"","<span class=\"pagenxt\" style=\"display: block;\"></span>"));
		pageHtml.append("</ul>");
		pageHtml.append("</div>");
		pageHtml.append("</div>");
		pageHtml.append("</form>");
		out.println(pageHtml);
		super.doTag();
	}
	public StringBuffer getParams(StringBuffer pageHtml) {
		// 设置参数用map方法设置参数 和值
		if (paramMap != null) {
			Set<String> keyset = paramMap.keySet();
			for (Iterator<String> iterator = keyset.iterator(); iterator
					.hasNext();) {
				String paramKey = (String) iterator.next();// 参数名
				String paramValue = (String) paramMap.get(paramKey);// 参数值
				pageHtml.append("<input type=\"hidden\" id=\"" + paramKey + "\" name=\"" + paramKey + "\" value=\"" + paramValue + "\">");
			}
		}
		return pageHtml;
	}
	public String getPath() {
		return path;
	}

	public void setPath(String path) {
		this.path = path;
	}

	public PageBean getPageBean() {
		return pageBean;
	}

	public void setPageBean(PageBean pageBean) {
		this.pageBean = pageBean;
	}
	public Map<String, String> getParamMap() {
		return paramMap;
	}
	public void setParamMap(Map<String, String> paramMap) {
		this.paramMap = paramMap;
	}
	public String getPageFormId() {
		return pageFormId;
	}
	public void setPageFormId(String pageFormId) {
		this.pageFormId = pageFormId;
	}

}
